﻿// Autore: Mattia Barrasso

using System.Collections.Generic;
using System.Threading;
using NUnit.Framework;
using RecipesManager.Data;
using White.Core;
using White.Core.InputDevices;
using White.Core.UIItems;
using White.Core.UIItems.Finders;
using White.Core.UIItems.ListBoxItems;
using White.Core.UIItems.WPFUIItems;
using White.Core.UIItems.WindowItems;

namespace RecipesManager.Specs.Ospite
{
    public class OspiteBdd
    {
        protected const string ShopManagerTitle = "Gestione Ospiti";
        protected Dictionary<string, int> Eventi;

        protected Window MainWindow;
        protected Application Application;
        protected Button NewGuest;
        protected Button RemoveGuest;
        protected Button BackToMainMenu;
        protected ListBox Guests;

        protected TextBox GuestName;
        protected TextBox GuestNote;
        protected ComboBox GuestsPreferences;
        protected IUIItem SelectedGuestsPreferences;
        protected Button AddGuestPreference;
        protected Button RemoveGuestPreference;
        protected ComboBox FoodPreferences;
        protected IUIItem SelectedFoodPreferences;
        protected Button AddFoodPreference;
        protected Button RemoveFoodPreference;

        protected Button Close;
        protected Button Save;
        protected Button ViewStat;

        protected void GetSingleGuestUi()
        {
            GuestName = MainWindow.Get<TextBox>("tb_nomeOspite");
            GuestNote = MainWindow.Get<TextBox>("tb_notes");

            GuestsPreferences = MainWindow.Get<ComboBox>("cb_persone");
            SelectedGuestsPreferences = MainWindow.Get<ListView>("lv_persons");
            AddGuestPreference = MainWindow.Get<Button>("bt_add_person");
            RemoveGuestPreference = MainWindow.Get<Button>("bt_del_person");

            FoodPreferences = MainWindow.Get<ComboBox>("cb_ingrcatric");
            SelectedFoodPreferences = MainWindow.Get<ListView>("lv_tastes");
            AddFoodPreference = MainWindow.Get<Button>("bt_add_taste");
            RemoveFoodPreference = MainWindow.Get<Button>("bt_del_taste");

            Close = MainWindow.Get<Button>("bt_chiudi");
            Save = MainWindow.Get<Button>("bt_salva");
            ViewStat = MainWindow.Get<Button>("bt_vis_stat");
        }

        protected void GetGuestsUi()
        {
            NewGuest = MainWindow.Get<Button>("bt_add");
            RemoveGuest = MainWindow.Get<Button>("bt_delete");
            BackToMainMenu = MainWindow.Get<Button>("bt_back");
            Guests = MainWindow.Get<ListBox>("lb_items");
        }

        protected void SetUpScenario()
        {
            Eventi = new Dictionary<string, int>();
            Manager.ClearDatabase();
            Manager.Measures.Add("grammi", "gr", MeasureType.Weight, MeasureFamily.Metric, 1);
            Manager.Measures.Add("PesoTest", "PT", MeasureType.Weight, MeasureFamily.Metric, 1);
            Application = Application.Launch(@"..\..\..\RecipesManager\bin\x86\WhiteTesting\RecipesManager.exe");
            MainWindow = Application.GetWindow("Recipes Manager");
        }

        protected void TearDownScenario()
        {
            Eventi.Clear();
            Manager.ClearCache();
            Manager.ClearDatabase();
            Application.Kill();
        }

        protected void NavigateToMainMenu()
        {
            MainWindow.Get<Button>("bt_back").Click();
            MainWindow = Application.GetWindow("Recipes Manager");
        }

        protected void NavigateToGuests()
        {
            MainWindow.Get<Button>("ospiti").Click();
            MainWindow = Application.GetWindow(ShopManagerTitle);
        }

        protected int CercaPersonaPreferita(string persona)
        {
            var lw = (ListView)SelectedGuestsPreferences;
            int i;
            for (i = 0; i < lw.Rows.Count; i++)
            {
                Thread.Sleep(200);
                var element = lw.Rows[i].GetElement(SearchCriteria.ByText(persona));
                if (element != null) 
                    return i;
            }
            return -1;
        }

        protected int CercaCiboPreferito(string cibo)
        {
            //MainWindow.ReInitialize(InitializeOption.NoCache);
            //Thread.Sleep(1500);
            var lw = (ListView)SelectedFoodPreferences;
            int i;
            for (i = 0; i < lw.Rows.Count; i++)
            {
                var element = lw.Rows[i].GetElement(SearchCriteria.ByText(cibo));
                if (element != null) return i;
            }
            return -1;
        }

        protected void SelezionaPersonaPreferita(string persona)
        {
            var i = CercaPersonaPreferita(persona);
            var lw = (ListView) SelectedGuestsPreferences;
            Mouse.instance.Location = lw.Rows[i].GetElement(SearchCriteria.All).GetClickablePoint();
            Mouse.instance.Click();
        }

        protected void ModificaPreferenzaPersona(string persona, int index)
        {
            var i = CercaPersonaPreferita(persona);
            var lw = (ListView)SelectedGuestsPreferences;
            var comboBox = lw.Rows[i].Get<ComboBox>(SearchCriteria.All);
            comboBox.Select(index);
            while(!comboBox.SelectedItem.Text.Equals(comboBox.Items[index].Text))
            {
                comboBox.Select(index);
            }
        }

        protected void ImpostaPersonaComeIndispensabile(string persona)
        {
            var i = CercaPersonaPreferita(persona);
            var lw = (ListView)SelectedGuestsPreferences;
            var checkBox = lw.Rows[i].Get<CheckBox>(SearchCriteria.All);
            Assert.That(!checkBox.Checked);
            checkBox.Toggle();
        }

        protected void ImpostaPersonaComeNonIndispensabile(string persona)
        {
            var i = CercaPersonaPreferita(persona);
            var lw = (ListView)SelectedGuestsPreferences;
            var checkBox = lw.Rows[i].Get<CheckBox>(SearchCriteria.All);
            Assert.That(checkBox.Checked);
            checkBox.Toggle();
        }

        protected void ImpostaCiboComeTollerato(string cibo)
        {
            var i = CercaCiboPreferito(cibo);
            var lw = (ListView)SelectedFoodPreferences;
            var checkBox = lw.Rows[i].Get<CheckBox>(SearchCriteria.All);
            Assert.That(checkBox.Checked);
            checkBox.Toggle();
        }

        protected void ImpostaCiboComeNonTollerato(string cibo)
        {
            var i = CercaCiboPreferito(cibo);
            var lw = (ListView)SelectedFoodPreferences;
            var checkBox = lw.Rows[i].Get<CheckBox>(SearchCriteria.All);
            Assert.That(!checkBox.Checked);
            checkBox.Toggle();
        }

        protected void SelezionaCiboPreferito(string cibo)
        {
            var i = CercaCiboPreferito(cibo);
            var lw = (ListView)SelectedFoodPreferences;
            Mouse.instance.Location = lw.Rows[i].GetElement(SearchCriteria.All).GetClickablePoint();
            Mouse.instance.Click();
        }

        protected void ModificaPreferenzaCibo(string cibo, int index)
        {
            var i = CercaCiboPreferito(cibo);
            var lw = (ListView)SelectedFoodPreferences;
            var comboBox = lw.Rows[i].Get<ComboBox>(SearchCriteria.All);
            comboBox.Select(index);
            while (!comboBox.SelectedItem.Text.Equals(comboBox.Items[index].Text))
            {
                comboBox.Select(index);
            }
        }

    }
}
